chmod command
chmod
- change file mode bits
The chmod
command in Linux is used to change file permissions for owners, groups, and others. It controls who can read, write, or execute a file or directory, making it essential for security and access management.
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
MODE
: The permissions to set (symbolic likeu+x
or numeric like755
).FILE
: The target file(s) or directory(s).OPTION
: Flags to modify behavior (e.g.,-R
for recursive).
Examples
-
Understanding Permissions
Linux permissions are divided into:
- Users: Owner (
u
), Group (g
), Others (o
), All (a
). - Types: Read (
r
, 4), Write (w
, 2), Execute (x
, 1`).
Check permissions with
ls -l
:-rwxr-xr-- 1 alice users 123 Apr 1 10:00 script.sh
-rwxr-xr--
: Owner (rwx
), Group (r-x
), Others (r--
).
- Users: Owner (
-
Symbolic Mode
Use letters and operators to modify permissions.
Operators:
+
: Add permission.-
: Remove permission.=
: Set exact permission.
Example (Add Execute):
chmod u+x script.sh
- Adds execute (
x
) for the owner (u
). - Before:
-rw-r--r--
, After:-rwxr--r--
.
Example (Remove Write):
chmod g-w file.txt
- Removes write (
w
) for the group (g
).
Example (Set Exact):
chmod o=r file.txt
- Sets others (
o
) to read-only (r
).
Multiple Changes:
chmod u+rwx,g+rx,o-rwx secret.txt
- Owner: read/write/execute; Group: read/execute; Others: none.
-
Numeric (Octal) Mode
Use a 3-digit number to set permissions for owner, group, and others:
r
= 4,w
= 2,x
= 1 (add them up).- Examples:
7
=rwx
(4+2+1).5
=r-x
(4+1).0
=---
.
chmod 755 script.sh
7
(owner:rwx
),5
(group:r-x
),5
(others:r-x
).- Result:
-rwxr-xr-x
.
Example (Read-Only):
chmod 644 file.txt
6
(owner:rw-
),4
(group:r--
),4
(others:r--
).- Result:
-rw-r--r--
.
-
Recursive Changes
Use
-R
to apply permissions to a directory and its contents.chmod -R 755 /var/www
- Sets
rwx
for owner,r-x
for group/others on/var/www
and all files inside.
- Sets
-
Verbose Output
Use
-v
to see what changes are made.chmod -v u+x script.sh
- Output:
mode of 'script.sh' changed from 0644 (rw-r--r--) to 0744 (rwxr--r--)
.
- Output:
-
Common Permissions Examples
Mode | Symbolic | Use Case |
---|---|---|
755 | rwxr-xr-x | Executables (scripts, binaries) |
644 | rw-r--r-- | Regular files |
700 | rwx------ | Private scripts for owner only |
777 | rwxrwxrwx | Full access (use cautiously) |
To get help related to the chmod
command use --help
option
$ chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
or: chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display this help and exit
--version output version information and exit
Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.
For more details, check the manual with man chmod